﻿using System;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace CSSMixer
{
    public partial class MainForm : Form
    {
        private string _folderPath = string.Empty;

        public MainForm()
        {
            InitializeComponent();
        }

        private void OpenFoldertoolStripButton_Click(object sender, EventArgs e)
        {
            ThemefolderBrowserDialog.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            if (ThemefolderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                _folderPath = ThemefolderBrowserDialog.SelectedPath;
                SelectedFolderlabel.Text = "Selected CSS source Folder : " + _folderPath;
                ShowCSSFiles(_folderPath);
            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(CssResultrichTextBox.Text))
            {
                MessageBox.Show("There is no CSS to save!\n\nOpen a CSS folder, make an action the save", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            CSSsaveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            
            if (CSSsaveFileDialog.ShowDialog() == DialogResult.OK)
            {
                StreamWriter streamWriter = new StreamWriter(CSSsaveFileDialog.FileName);
                streamWriter.Write(CssResultrichTextBox.Text);
                streamWriter.Close();
            }
        }

        private void ShowCSSFiles(string _folderPath)
        {
            CSScheckedListBox.Items.Clear();
            string[] cssFiles = Directory.GetFiles(_folderPath, "*.css", SearchOption.AllDirectories);

            foreach (string cssFile in cssFiles)
            {
                CSScheckedListBox.Items.Add(new CheckItem(Path.GetFileName(cssFile), cssFile), true);
            }
            CountFilestoolStripStatusLabel.Text = string.Format("There are {0} files. {1}",cssFiles.Length,(cssFiles.Length>30?"There are more than 30 files, if you use Internet explorer you have to combine them!":""));
        }


        private void toolStripLabel3_Click(object sender, EventArgs e)
        {
            Process.Start("http://www.sambeauvois.be/blog");
        }

        private void CombinetoolStripButton_Click(object sender, EventArgs e)
        {
            if (!CheckValidity())
            {
                return;
            }

            CssCombiner combiner = new CssCombiner();
            combiner.AddHeader = true;
            foreach (CheckItem checkItem in CSScheckedListBox.CheckedItems)
            {
                combiner.Add(checkItem.FilePath);
            }
            CssResultrichTextBox.AppendText(combiner.Combine());
            ShowTextInfos();
        }

        private void MinimifytoolStripButton_Click(object sender, EventArgs e)
        {
            if (!CheckValidity())
            {
                return;
            }

            CssCompressor compressor = new CssCompressor();
            if (!string.IsNullOrEmpty(CssResultrichTextBox.Text))
            {
                CssResultrichTextBox.Text = compressor.Compress(CssResultrichTextBox.Text);
            }
            else
            {
                foreach (CheckItem checkItem in CSScheckedListBox.CheckedItems)
                {
                    compressor.Add(checkItem.FilePath);
                }
                CssResultrichTextBox.Text = compressor.Compress();
            }
            ShowTextInfos();
        }

        private bool CheckValidity()
        {
            if (string.IsNullOrEmpty(_folderPath))
            {
                MessageBox.Show("There is no selected folder!\n\nSelect one then do the action", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (CSScheckedListBox.CheckedItems.Count == 0)
            {
                MessageBox.Show("There is no checked CSS!\n\nCheck one or more CSS in the list then do the action", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            return true;
        }

        private void ShowTextInfos()
        {
            string[] words = CssResultrichTextBox.Text.Split(' ');

            TextInfostoolStripStatusLabel.Text = string.Format("{0} lines, {1} words, {2} characters", CssResultrichTextBox.Lines.Length, words.Length, CssResultrichTextBox.Text.Length);
        }
    }
}
